<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="minimum-scale=1.0, width=device-width, maximum-scale=1.0, user-scalable=no"/>
    <meta charset="utf-8">
    <script src="https://unpkg.com/react@16/umd/react.development.js"></script>
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
    <title>Libraries</title>
</head>
<body>
<div id="react-container"></div>
  <script type="text/babel">

    const { Component } = React
    const { render } = ReactDOM

    const DataComponent = (ComposedComponent, url) =>
      class DataComponent extends Component {

          constructor(props) {
              super(props)
              this.state = {
                  data: [],
                  loading: false,
                  loaded: false
              }
          }

          componentWillMount() {
              this.setState({loading:true})
              fetch(url)
                  .then(response => response.json())
                  .then(data => this.setState({
                      loaded: true,
                      loading: false,
                      data
                  }))
          }

          render() {
              return (
                  <div className="data-component">
                      {(this.state.loading) ?
                          <div>Loading...</div> :
                          <ComposedComponent {...this.state}
                                             {...this.props} />}
                  </div>
              )
          }
      }

    const CountryNames = ({data, selected=""}) =>
      <select className="people-list" defaultValue={selected}>
         {data.map(({name}, i) =>
             <option key={i} value={name}>{name}</option>
         )}
     </select>

    const CountryDropDown = DataComponent(
                              CountryNames,
                              "https://restcountries.eu/rest/v1/all"
                          )
    render(
      <CountryDropDown selected="United States" />,
      document.getElementById('react-container')
    )


  </script>
</body>
</html>
